<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/31
 * Time: 14:37
 */
namespace Admin\Controller;
use Think\Controller;

class MasterMaintainController extends CommonController
{
    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='财务管理';
        $this->breadcrumb2='师傅维修金管理';
        $this->url=U('MasterMaintain/index');
        $this->sqlname='mastermaintain'.session('user_auth')['uid'];
    }

    public function index(){
        $date1=I('post.time1','','string');
        $date2=I('post.time2','','string');
        $order_id=I('post.order_id');
        $master_id=I('post.master_id','','string');
        $master_name=I('post.master_name','','string');
        $money_type=I('post.money_type','','int');
        $where['ro.pay_time']=array('neq',0);

        if($date1 && $date2){
            $date1=strtotime($date1);
            $date2=strtotime($date2);
            $where['ro.pay_time']=array('between',array($date1,$date2));
        }

        if($order_id){
            $where['ro.repair_order_num_alias']=$order_id;
        }

        if($master_id){
            $where['ro.worker_tel']=$master_id;
        }

        if($master_name){
            $where['ifo.id_name']=array('like',"%$master_name%");
        }

        if($money_type==1 || $money_type==2 ){
            $where['ro.money_type']=$money_type;
        }


        $where['pro.status']=array('eq',0);
        $count= M('repair_order ro')
            ->join('left join zsf_user_real_info ifo on ro.worker_user_id=ifo.user_id')
            ->join('left join zsf_repair_order_protocol pro on pro.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->count();
        $Page       = new \Think\Page($count,25);
        //记录sql语句 导出excel用
        $sql= M('repair_order ro')
            ->join('left join zsf_user_real_info ifo on ro.worker_user_id=ifo.user_id')
            ->join('left join zsf_repair_order_protocol pro on pro.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->field('ro.repair_order_num_alias,ro.worker_tel,ifo.id_name,pro.maintain,ro.pay_time,ro.money_type')
            ->select(false);

        $redis = S(array('type' => 'redis'));
        S($this->sqlname, $sql);

        $total=M('repair_order ro')
            ->join('left join zsf_user_real_info ifo on ro.worker_user_id=ifo.user_id')
            ->join('left join zsf_repair_order_protocol pro on pro.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->field('sum(pro.maintain) total')
            ->find();
        $list= M('repair_order ro')
            ->join('left join zsf_user_real_info ifo on ro.worker_user_id=ifo.user_id')
            ->join('left join zsf_repair_order_protocol pro on pro.order_id=ro.repair_order_num_alias')
            ->where($where)
            ->field('ro.repair_order_num_alias,ro.worker_tel,ifo.id_name,pro.maintain,ro.pay_time,ro.money_type')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();


        $show       = $Page->show();
        $this->assign('list',$list);
        $this->assign('money_type',$money_type);
        $this->assign('total',$total['total']);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function excelout(){
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname);
        //设置列名称
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'订单编号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'师傅账号','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'师傅名称','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'维修金（元）','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'维修金支付时间','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'到账状态','size'=>'20'),
        );
        //设置标题
        $title='师傅维修金管理';
        //选择类型
        $type=3;
        //执行sql
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;
    }
}